package update;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

public class Updater {
	
	public static final String BASE_UPDATE_URL = "https://infinitewars.googlecode.com/svn/trunk/";
	
	public static final String BASE_UPDATE_DIR = "";
	public static final String LATEST_VERSION_URL = BASE_UPDATE_URL + "updates/latestVersion.txt";
	
	public static final String THIS_VERSION_FILE = "data/thisVersion.txt";
	public static final String LATEST_VERSION_FILE = "data/latestVersion.txt";
	
	public static final String UPDATER_FILE = "updater.jar";
	public static final String META_LAUNCHER_FILE = "metaLauncher.jar";
	public static final String UPDATER_TEMP_FILE = "updaterTemp.jar";
	
	public static final String ADD_FILE = "addedFiles.txt";
	public static final String DELETE_FILE = "deletedFiles.txt";
	public static final String DIRECTORIES = "directories.txt";
	public static final String VERSION_FILE = "version.txt";
		
	private String systemName;
	
	public static void main(String[] args) {
		new Updater(args[0]);
	}
	
	public Updater(String systemName) {
		this.systemName = systemName;
		
		String thisVersion = getThisVersion();
		String latestVersion = getLatestVersion();
		
		if(thisVersion.equals(latestVersion)) startGame(false);
		else new UpdateWindow(this, thisVersion, latestVersion);
	}
	
	public void startGame(boolean updaterUpdated) {
		try {
			List<String> command = new ArrayList<String>();
			
			if(updaterUpdated) {
				command.add("java");
				command.add("-jar");
				command.add(META_LAUNCHER_FILE);
				command.add("metaLaunch.MetaLauncher");
				command.add(systemName);
			} else {
				command.add("java");
				command.add("-cp");
				command.add("infinitewars.jar");
				command.add("-Djava.library.path=./infinitewars_lib/native/" + systemName);
				command.add("main.InitGame");
			}
			
			ProcessBuilder builder = new ProcessBuilder(command);
			builder.start();
			
			System.exit(0);
		} catch(Throwable t) {
			t.printStackTrace();
		}
	}
	
	public void saveLatestVersionInfoFile(String latestVersion) {
		BufferedWriter writer = null;
		
		try {
			writer = new BufferedWriter(new FileWriter(new File(THIS_VERSION_FILE)));
			writer.write(latestVersion);
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(writer != null) {
				try { writer.close(); }
				catch(Exception e) { e.printStackTrace(); }
			}
		}
	}
	
	public String getLatestVersion() {
		DownloadProcess process = new DownloadProcess(LATEST_VERSION_URL, LATEST_VERSION_FILE);
		process.startDownload();
		
		String newVersion = readFile(LATEST_VERSION_FILE);
		
		File file = new File(LATEST_VERSION_FILE);
		file.delete();
		
		return newVersion;
	}
	
	public static String getThisVersion() {
		return readFile(THIS_VERSION_FILE);
	}
	
	public static String readFile(String filePath) {
		BufferedReader reader = null;
		
		try {
			reader = new BufferedReader(new FileReader(new File(filePath)));
			
			String content = reader.readLine();
			reader.close();
			
			return content;
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(reader != null) {
				try { reader.close(); }
				catch(Exception e) { e.printStackTrace(); }
			}
		}
		return null;
	}
	
	public List<String> getList(String url, String fileName) {
		DownloadProcess process = new DownloadProcess(url, fileName);
		process.startDownload();
		
		List<String> files = new ArrayList<String>();
		
		BufferedReader reader = null;
		File file = null;
		
		try {
			file = new File(fileName);
			reader = new BufferedReader(new FileReader(file));
			
			while(reader.ready()) {
				String newURL = reader.readLine();
				files.add(newURL);
			}
			
			reader.close();
			file.delete();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(reader != null) {
				try { reader.close(); }
				catch(Exception e) { e.printStackTrace(); }
			}
			if(file != null) file.delete();
		}
		
		return files;
	}
	
	public String getUpdatedVersion(String currentVersion) {
		String url = getVersionURL(currentVersion);
		String localFile = BASE_UPDATE_DIR + VERSION_FILE;
		DownloadProcess process = new DownloadProcess(url, localFile);
		process.startDownload();
		
		String version = readFile(localFile);
		
		File f = new File(localFile);
		f.delete();
		
		return version;
	}
	
	public static String getAddedFilesURL(String currentVersion) {
		return BASE_UPDATE_URL + "updates/" + currentVersion + "/" + ADD_FILE;
	}
	
	public static String getDeletedFilesURL(String currentVersion) {
		return BASE_UPDATE_URL + "updates/" + currentVersion + "/" + DELETE_FILE;
	}
	
	public static String getDirectoriesURL(String currentVersion) {
		return BASE_UPDATE_URL + "updates/" + currentVersion + "/" + DIRECTORIES;
	}
	
	public static String getVersionURL(String currentVersion) {
		return BASE_UPDATE_URL + "updates/" + currentVersion + "/" + VERSION_FILE;
	}
	
	public String getSystemName() { return systemName; }
}